package com.thinkcar.redis.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisSerializationConfig {

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);

        // 使用高效的序列化器
        template.setKeySerializer(new StringRedisSerializer());

        // 值序列化器选择
        // 方案1：JSON序列化（可读性好）
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());

        // 方案2：二进制序列化（性能好）
        // template.setValueSerializer(new JdkSerializationRedisSerializer());

        // 方案3：自定义序列化
        // template.setValueSerializer(new ProtoBufRedisSerializer());

        return template;
    }
}